<template>
	<view class="container">
		<view class="header">
			<!-- <view class="title">
				<text class="">关于放弃参加广州市城乡居民社会医疗保险的承诺书</text>
			</view> -->
		</view>
		<view class="body">
			<!-- 表单开始 -->
			<u-form :model="form" ref="uForm">

				<u-form-item label="学号" prop="stu_id">
					<u-input v-model="form.stu_id" :disabled="true"/>
				</u-form-item>

				<u-form-item label="姓名" prop="name">
					<u-input v-model="form.name" :disabled="true"/>
				</u-form-item>

				<u-form-item label="专业" prop="major">
					<u-input v-model="form.major" :disabled="true"/>
				</u-form-item>
				
				<u-form-item label="身份证号码" label-position="top" prop="id">
					<u-input v-model="form.id"/>
				</u-form-item>

				<!-- <u-form-item label="联系方式" label-position="top" prop="tel">
					<u-input v-model="form.tel" />
				</u-form-item>
				
				<u-form-item label="家长联系方式" label-position="top" prop="tel">
					<u-input v-model="form.tel" />
				</u-form-item> -->

				<u-form-item label="已在他处购买医保" label-position="top" prop="region">
					<u-input placeholder="请选择地址(省市区)" v-model="form.region" type="select"
						@click="viewShow.region = true" />
				</u-form-item>

				<u-form-item label="县、乡、镇、街道" label-position="top" prop="regionInfo">
					<u-input v-model="form.regionInfo" />
				</u-form-item>


			</u-form>
			<!-- 表单结束 -->

			<view class="submit" style="margin-top: 80rpx;padding-bottom: 40rpx;">
				<u-button @click="submit">{{isSubmit?'修改申请':'提交申请'}}</u-button>
			</view>
		</view>

		<view class="foot">
			<text>注：本申请仅适用于已在<text
					style="color: #e96900;background-color: #f8f8f8;border-radius: 2px;padding: 1px;">广州市以外</text>的其他统筹地区购买了2022年度社会医疗保险的学生。</text>
					<text @click="showMoadl" style="color: #2196f3;background-color: #f8f8f8;border-radius: 2px;padding: 1px;">有疑问？</text>
		</view>
		<view class="pickerView">
			<u-picker mode="region" v-model="viewShow.region" @confirm="pickerConfirm"></u-picker>
		</view>
	</view>
</template>

<script>
	import down from '../../../js_sdk/download/downloadBlob.js'
	const db = uniCloud.database()
	export default {
		data() {
			return {
				isSubmit:false,
				stu_id: '',
				defaultCode: [],
				dc: [],
				region: false,
				viewShow: {
					region: false
				},
				form: {
					name: '',
					stu_id: '',
					major: '',
					tel: '',
					region: '',
					id:'',
					regionInfo: ''
				},
				rules: {
					name: [{
						required: true,
						message: '请输入姓名',
						trigger: ['change', 'blur']
					}],
					stu_id: [{
						required: true,
						message: '请输入学号',
						trigger: ['change', 'blur']
					}],
					id: [{
						required: true,
						message: '请输入身份证号码',
						trigger: ['change', 'blur']
					},{
						// 自定义验证函数，见上说明
						validator: (rule, value, callback) => {
							return this.$u.test.idCard(value);
						},
						message: '身份证号码不正确',
						// 触发器可以同时用blur和change
						trigger: ['blur'],
					}],
					major: [{
						required: true,
						message: '请输入专业',
						trigger: ['change', 'blur']
					}],
					tel: [{
						required: true,
						message: '请输入联系方式',
						trigger: ['change', 'blur']
					}, {
						// 自定义验证函数，见上说明
						validator: (rule, value, callback) => {
							return this.$u.test.mobile(value);
						},
						message: '手机号码不正确',
						// 触发器可以同时用blur和change
						trigger: ['change', 'blur'],
					}],
					region: [{
						required: true,
						message: '请选择地址',
						trigger: ['change', 'blur']
					}],
					regionInfo: [{
						required: true,
						message: '请输入详细地址',
						trigger: ['change', 'blur']
					}]
				}
			}
		},
		onLoad() {
			this.stu_id = uni.getStorageSync('stu_id')
			if (!this.stu_id) {
				uni.showToast({
					icon: 'error',
					title: '未绑定学号'
				})
				return
			}
			this.form.stu_id = this.stu_id
			uni.showLoading({
				title:'请求中',
			})
			//查询已提交信息
			uniCloud.callFunction({
				name: 'lifeServices',
				data: {
					action: 'yibao',
					dz: 'getOnloadList',
					params: {
						stu_id: this.stu_id
					},
				},
				success: (res) => {
					if(res.result.data[0]== undefined){
						uni.hideLoading()
						uni.showModal({
							title:'提示',
							content:'系统无该生在线信息,请联系相关负责人',
							success: (res) => {
								uni.navigateBack({
									
								})
							}
						})
						return
					}
					
					if(res.result.data[0].yibaoInfo.length != 0){
						this.isSubmit = true
						this.form = res.result.data[0].yibaoInfo[0]
						delete this.form['_id']
						uni.hideLoading()
						return
					}
					this.form.name = res.result.data[0].xm
					this.form.stu_id = res.result.data[0].xh
					this.form.major = res.result.data[0].zy
					uni.hideLoading()
					// delete this.form['_id']
				},
				fail: (err) => {
					uni.hideLoading()
					uni.showModal({
						title:'提示',
						content:err
					})
				}
			})
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			showMoadl(){
				uni.showModal({
					title:'提示',
					content:'如在已在学校外购买了广州医保,则学校会自动退费，无须进行退保！！！\n更多疑问联系相关负责人'
				})
			},
			submit() {
				this.$refs.uForm.validate((valid) => {
					if (valid) {
						console.log('验证通过')
						uni.showLoading({
							title:'正在提交'
						})
						uniCloud.callFunction({
							name: 'lifeServices',
							data: {
								action: 'yibao',
								dz: 'add',
								params: this.form
							},
							success: (res) => {
								if (res.result.code == 1) {
									console.log('提交成功')

								} else if (res.result.code == 0) {
									console.log('更新成功')

								}
								uni.hideLoading()
								uni.showToast({
									title: res.result.msg
								})
								
								let url = '../../success/success?res='+encodeURIComponent(res.result.msg)+'&tips='+encodeURIComponent('请下载纸质“退保承诺书”并打印签字')
								uniCloud.callFunction({
									name:'lifeServices',
									data:{
										action:'yibao',
										dz:'exportWord',
										params:{
											stu_id:this.stu_id
										},
									},
									success: (res) => {
										console.log(res)
										
										if(res.result.status == 200){
											// #ifdef H5
											setTimeout(()=>{
												uni.showToast({
													icon:'none',
													title:'请稍后，正在唤起下载窗口'
												})
											},100)
											// location.href = res.result.downUrl
											// 创建iframe唤起下载
											let src = res.result.downUrl
											let isIframe = document.getElementById('down1')
											// 若已被创建过
											if(isIframe){
												isIframe.src = "javascript: '<script>location.href=\""+src+"\"<\/script>'"
											}
											else{
												let iframe = document.createElement('iframe')
												iframe.id = 'down1'
												iframe.style.display = 'none'
												iframe.src = "javascript: '<script>location.href=\""+src+"\"<\/script>'"
												document.getElementsByTagName('body')[0].appendChild(iframe)
											}
											// document.getElementsByTagName('body')[0].removeChild(iframe)
											// #endif
										}
										
										uni.navigateTo({
											url:url
										})
									},
									fail: (err) => {
										// uni.hideLoading()
										uni.showModal({
											title:'提示',
											content:err
										})
										console.log(err)
									}
								})
								 
								
							},
							fail: (err) => {
								uni.showModal({
									title:'提示',
									content:err
								})
							}
						})
					} else {
						console.log('验证失败')
					}
				})
			},
			pickerConfirm(e) {


				if (e.city.value == 4401) {
					uni.showToast({
						icon: 'error',
						title: '不支持广州市'
					})
					this.form.region = ''

				} else {
					this.form.region = e.province.label + e.city.label + e.area.label
				}

			}
		}
	}
</script>

<style>
	.container {
		padding: 40rpx;
	}

	.title {
		padding-left: 80rpx;
		padding-right: 80rpx;
		font-weight: 700;
	}

	.body {}

	.body_text {}

	.foot {}

	.foot text {
		font-size: 15px;
	}
</style>
